package io.xxx.xbutler.core.callback;

import com.alibaba.fastjson2.JSONArray;
import com.alibaba.fastjson2.JSONObject;
import com.hy.corecode.idgen.WFGIdGenerator;
import io.xxx.xbutler.data.ContactMapper;
import io.xxx.xbutler.data.RobotContactMapper;
import io.xxx.xbutler.data.RoomContactMapper;
import io.xxx.xbutler.domain.Callback;
import io.xxx.xbutler.domain.Contact;
import io.xxx.xbutler.domain.GenderType;
import io.xxx.xbutler.domain.RobotContact;
import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;
import org.springframework.transaction.support.TransactionTemplate;

import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;

/**
 * 获取群成员信息回调
 * <p>
 * 获取单个群成员信息回调
 *
 * <pre>
 * {
 *     "nType": 4514,
 *     "vcMerchantNo": "",
 *     "vcRobotWxId": "",
 *     "vcRobotSerialNo": "",
 *     "vcSerialNo": "",
 *     "nResult": 1,
 *     "vcResult": "SUCCESS",
 *     "Data": [
 *         {
 *             "vcMemberUserWxId": "",
 *             "vcMemberUserSerialNo": "",
 *             "vcWxAlias": "r",
 *             "vcNickName": "",
 *             "vcBase64NickName": "",
 *             "vcHeadImages": "",
 *             "nSex": 2,
 *             "vcWhatsUp": "",
 *             "vcBase64WhatsUp": "",
 *             "vcCountry": "CN",
 *             "vcProvince": "Hunan",
 *             "vcCity": "Changsha",
 *             "vcLanguage": "CN",
 *             "vcFatherUserWxId": "",
 *             "vcFatherUserSerialNo": "",
 *             "vcGroupNickName": "",
 *             "vcBase64GroupNickName": "",
 *             "finder_V2": "v2_060000231003b20faec8c7ea8a1cc0d7cf07eb36b077c98bdaa6f86dbdd78d4c906509c047e7@finder"
 *         }
 *     ],
 *     "nTimeStamp": 1603855694960
 * }
 * </pre>
 */
@Slf4j
@Component
public class _4514Handler implements CallbackHandler {

    @Resource
    private WFGIdGenerator idGenerator;

    @Resource
    private TransactionTemplate transactionTemplate;

    @Resource
    private ContactMapper contactMapper;

    @Resource
    private RoomContactMapper roomContactMapper;

    @Resource
    private RobotContactMapper robotContactMapper;

    @Override
    public String name() {
        return "获取群成员信息回调";
    }

    @Override
    public boolean supports(int type) {
        return type == 4514;
    }

    @Override
    public void handle(Callback callback) {
        LocalDateTime now = LocalDateTime.now();
        JSONArray data = (JSONArray) callback.getData();

        List<Contact> contacts = new ArrayList<>(data.size());
        List<RobotContact> robotContacts = new ArrayList<>(data.size());
        for (Object datum : data) {
            JSONObject info = JSONObject.from(datum);
            Contact contact = new Contact()
                    .setId(info.getString("vcMemberUserWxId"))
                    .setSerialNo(info.getString("vcMemberUserSerialNo"))
                    .setAlias(info.getString("vcWxAlias"))
                    .setName(info.getString("vcNickName"))
                    .setBase64Name(info.getString("vcBase64NickName"))
                    .setAvatar(info.getString("vcHeadImages"))
                    .setGender(GenderType.valueOf(info.getIntValue("nSex")))
                    .setWhatsUp(info.getString("vcWhatsUp"))
                    .setBase64WhatsUp(info.getString("vcBase64WhatsUp"))
                    .setProvince(info.getString("vcProvince"))
                    .setCity(info.getString("vcCity"))
                    .setCounty(info.getString("vcCountry"));
            contacts.add(contact);

            RobotContact robotContact = new RobotContact()
                    .setId(idGenerator.next())
                    .setContactId(info.getString("vcMemberUserWxId"))
                    .setContactSerialNo(info.getString("vcMemberUserSerialNo"))
                    .setRobotId(callback.getRobotId())
                    .setRobotSerialNo(callback.getRobotSerialNo())
                    .setCreatedTime(now)
                    .setUpdatedTime(now);
            robotContacts.add(robotContact);

            transactionTemplate.executeWithoutResult(_ -> {
                contactMapper.insert(contacts);
                robotContactMapper.insert(robotContacts);
            });
        }
    }
}
